<?php

namespace app\shop\controller\api;

use app\ApiController;
use app\distribution\model\DividendAwardModel;
use think\Db;

use app\shop\model\CartModel;
use app\mainadmin\model\PaymentModel;
use app\member\model\UserAddressModel;
use app\member\model\AccountLogModel;
use app\shop\model\BonusModel;
use app\shop\model\OrderModel;
use app\shop\model\GoodsModel;
use app\shop\model\BonusListModel;
use app\weixin\model\WeiXinUsersModel;
use app\member\model\UsersModel;
use app\distribution\model\DividendRoleModel;
/*------------------------------------------------------ */
//-- 购物相关API
/*------------------------------------------------------ */

class Flow extends ApiController
{
    public $is_integral = 0;
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new CartModel();
         $this->Model->is_integral = $this->is_integral;
    }
    
    public function test(){
        (new DividendAwardModel())->td_award(80);
        // (new DividendRoleModel())->uplevel('29900');
        // (new DividendAwardModel())->yjup(19989,249);
    }
    /*------------------------------------------------------ */
    //-- 添加购物车
    /*------------------------------------------------------ */
    public function addCart()
    {
        $goods_id = input('goods_id', 0, 'intval');
        $num = input('number', 1, 'intval');
		$type = input('type','','trim');
		if ($type == 'onbuy'){
			$this->checkLogin();//验证登陆
		}
        if ($num < 1) $num = 1;
        $specifications = input('specifications', '', 'trim');
        $sku_id = input('sku_id', 0, 'intval');
        if ($specifications == 'undefined') $specifications = '';
        if ($goods_id < 1) return $this->error('传值失败，请重新尝试提交！');
        //检查限购
        $goods = Db::name('shop_goods')->where('goods_id',$goods_id)->find();
       
        if($goods['restrictions'] > 0){
            //用户已购买
            $shop = Db::name('shop_order_info')
            ->alias('o')
            ->join('shop_order_goods og','o.order_id = og.order_id')
            ->where('og.goods_id',$goods_id)
            ->where('o.user_id',$this->userInfo['user_id'])
            ->where('o.pay_status',1)
            ->field('og.goods_number')
            ->select();
            $numb = 0;
            foreach($shop as $v){
                $numb += $v['goods_number'];
            }
            if($goods['restrictions'] < $numb+$num){
                return $this->error('该商品限购 ！');
            }
        }
        if($goods['restrictionsmothe'] > 0){
            //用户已购买
            $shop = Db::name('shop_order_info')
            ->alias('o')
            ->join('shop_order_goods og','o.order_id = og.order_id')
            ->where('og.goods_id',$goods_id)
            ->where('o.user_id',$this->userInfo['user_id'])
            ->where('o.pay_status',1)
            ->whereTime('o.pay_time', 'month')
            ->field('og.goods_number')
            ->select();
            $numb = 0;
            foreach($shop as $v){
                $numb += $v['goods_number'];
            }
            if($goods['restrictionsmothe'] < $numb+$num){
                return $this->error('本月购买商品已达上限 ！');
            }
        }
        // 检查复购等级是否够
        if($goods['cid'] == 51 and $this->userInfo['role_id'] < 6){
            return $this->error('您的等级不能购买复购商品 ！');
        }
        $rec_id = $this->Model->addToCart($goods_id, $num, $specifications,$sku_id,$type);
        if (is_numeric($rec_id) == false) {
            return $this->error($rec_id);
        }
        if ($type != 'onbuy') {
            $return['cartInfo'] = $this->Model->getCartInfo();
            $return['msg'] = '添加购物车成功.';
        }
        $return['code'] = 1;
		$return['rec_id'] = $rec_id;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 获取购物车列表
    /*------------------------------------------------------ */
    public function getList()
    {
        $is_sel = input('is_sel', 0, 'intval');
		$recids = input('recids', '', 'trim');
        $return['cartInfo'] = $this->Model->getCartList($is_sel,false,$recids);
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 获取购物车信息
    /*------------------------------------------------------ */
    public function getCartInfo()
    {
        $return['cartInfo'] = $this->Model->getCartInfo();
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 修改商品订购数量
    /*------------------------------------------------------ */
    function editNum()
    {
        $rec_id = input('rec_id', 0, 'intval');
        $is_sel = input('is_sel', 0, 'intval');
		$recids = input('recids', '', 'trim');
        if ($rec_id < 1) return $this->error('传值失败，请重新尝试提交.');
        $num = input('num', 1, 'intval');
        if ($num < 1) return $this->error('订购数量不能小于1.');
        $where['rec_id'] = $rec_id;
        $res = $this->Model->updataGoods($rec_id, $num);
        if ($res != 1){
			if ($res === 0) return $this->error('');
			return $this->error($res);
		}
        $address_id = input('address_id', 0, 'intval');
        if ($address_id > 0) {
            $return['shippingFee'] = $this->evalShippingFee($address_id, true,$recids);
        }
        $return['cartInfo'] = $this->Model->getCartList($is_sel,true,$recids);
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 删除购物车的商品
    /*------------------------------------------------------ */
    function delGoods()
    {
        $rec_id = input('rec_id', 0, 'intval');
        if ($rec_id < 1) return $this->error('传值失败，请重新尝试提交！');
        $this->Model->delGoods($rec_id);
        $return['cartInfo'] = $this->Model->getCartList();
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 清除购物车无效的商品
    /*------------------------------------------------------ */
    function clearInvalid()
    {
        $this->Model->clearInvalid();
        $return['cartInfo'] = $this->Model->getCartList();
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 设置商品是否选中
    /*------------------------------------------------------ */
    function setSel()
    {
        $rec_id = input('rec_id');
        if ($rec_id < 1 && $rec_id != 'all') return $this->error('传值失败，请重新尝试提交！');
        $is_select = input('is_select', 0, 'intval');
        $res = $this->Model->updateCart($rec_id, ['is_select' => $is_select]);
        $return['cartInfo'] = $this->Model->getCartList();
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 删除选中的商品
    /*------------------------------------------------------ */
    function delSelGoods()
    {
        $rec_id = input('');
        if (!$rec_id ) return $this->error('传值失败，请重新尝试提交！');
        $this->Model->delGoods($rec_id);
        $return['cartInfo'] = $this->Model->getCartList();
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }

    /*------------------------------------------------------ */
    //-- 计算运费
    /*------------------------------------------------------ */
    function evalShippingFee($address_id = 0, $is_return = false,$recids = '')
    {
        if ($address_id < 0) {
            $address_id = input('address_id', '0', 'intval');
			$recids = input('recids', '', 'trim');
        }
		$return['code'] = 1;
        if ($address_id < 1) {
            $return['shipping_fee'] = sprintf("%.2f", 0);
			if ($is_return == true) return $return;
            return $this->ajaxReturn($return);
        }
		$UserAddressModel = new UserAddressModel();
        $addressList = $UserAddressModel->getRows();
        $address = $addressList[$address_id];
		$cartList = $this->Model->getCartList(1, true,$recids);
        $shippingFee = $this->Model->evalShippingFee($address,$cartList);
        $shippingFee = reset($shippingFee);//现在只返回默认快递
        $shippingFee['shipping_fee'] = sprintf("%.2f", $shippingFee['shipping_fee'] * 1);
        if ($is_return == true) return $shippingFee;
        $return['shippingFee'] = $shippingFee;
        return $this->ajaxReturn($return);
    }


    /*------------------------------------------------------ */
    //-- 付款 执行下单
    /*------------------------------------------------------ */
    function addSotreOrder()
    {
//        $this->checkLogin();//验证登录

//        dump($_SERVER['HTTP_USER_AGENT']);exit;
        $user_id = input('user_id');
        if ($user_id < 0) return $this->error('门店不存在请核对后再操作.');

        $amount = input('amount', '', 'trim');
        if ($amount < 0) return $this->error('输入金额有误.');

      $distribution_store = Db::name('distribution_store')->where(['user_id'=>$user_id,'status'=>1])->find();
      if (empty($distribution_store)) return $this->error('门店没有该权限.');
        $OrderModel = new OrderModel();

        $inArr['order_status'] = 0;  // 订单状态
        $inArr['pay_status'] = 0;  // 支付状态
        $_log = '线下门店支付生成订单';

        $inArr['supplyer_id'] = $distribution_store['supplyer_id'];  // 门店ID
        $inArr['store_id'] = $distribution_store['store_id'];;  // 门店ID
        $inArr['order_sn'] = $OrderModel->getOrderSnVshop();  // 订单编号
        $inArr['user_id'] = $user_id;
        $inArr['pay_code'] = 'weixin';
        $inArr['order_amount'] = $amount;  // 订单金额
        $inArr['add_time'] = time();
        $inArr['ipadderss'] =  request()->ip();
//        dump($inArr);exit;
        Db::startTrans();//启动事务

        $re = Db::name('store_order')->insertGetId($inArr);
        if ($re < 1) {
            Db::rollback();// 回滚事务
            return $this->error('未知原因，订单写入失败.');
        }

        $inArr['order_id'] = $re;
        $inArr['shipping_status'] = 0;
//        $res = $OrderModel->_log($inArr,$_log);
        if ($re < 1) {
            Db::rollback();// 回滚事务
            return $this->error('未知原因，订单日志写入失败.');
        }

        //end
        Db::commit();// 提交事务
//        if(strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')){
//            dump(1);exit;
//        }else{
//            dump(2);exit;
//        }
//        dump($res);exit;
        $return['order_id'] =  $inArr['order_id'];
        $return['code'] = 1;
        $return['order_sn'] = $inArr['order_sn'];
        $return['amount'] = $amount;
        return $this->ajaxReturn($return);
    }


    /*------------------------------------------------------ */
    //-- 执行下单
    /*------------------------------------------------------ */
    function addOrder()
    {
        $this->checkLogin();//验证登录

        $address_id = input('address_id', 0, 'intval');
        if ($address_id < 0) return $this->error('请设置收货地址后，再操作.');
        $UserAddressModel = new UserAddressModel();
        $addressList = $UserAddressModel->getRows();
        $address = $addressList[$address_id];
        if (empty($address)) return $this->error('相关收货地址不存在.');
        $used_bonus_id = input('used_bonus_id', 0, 'intval');

		$recids = input('recids', '', 'trim');
        $cartList = $this->Model->getCartList(1, true,$recids,false,false);

        if ($cartList['buyGoodsNum'] < 1) return $this->error('请选择订购商品.');

        $password = input('password', 0, 'intval');
        $data['pay_password'] = f_hash($password);
        if ($data['pay_password'] != $this->userInfo['pay_password']){
            return $this->error('支付密码错误.');
        }
        $GoodsModel = new GoodsModel();
        $supplyer_ids = [];//供应商ID

        $inArr['give_integral'] = 0;
        $inArr['settle_price'] = 0;
        $use_integral = 0;
        $rec_ids = [];
        $allGoodsSn = [];
        $allGoodsId = [];
        
        // 验证购物车中的商品能否下单
        foreach ($cartList['goodsList'] as $grow) {
            $goods = $GoodsModel->info($grow['goods_id']);
            if($goods['is_premiums'] == 1){
                if($this->userInfo['role_id'] < 6){
                    return $this->error('合伙人身份才能购买免费产品');
                }
                    $WeiXinUsersModel =new WeiXinUsersModel();
                    $subscribe = $WeiXinUsersModel->where(['user_id' => $this->userInfo['user_id']])->value('subscribe');
                    if($subscribe !== 1){
                        return $this->error('请先关注公众号');
                    }
                    $OrderModel = new OrderModel();
                    $is_premiums = $OrderModel->is_premiums($this->userInfo['user_id']);
                    if($is_premiums){
                        return $this->error('您已经购买过免费产品');
                    }

            }
            // 判断是商品能否购买或修改
            $res = $this->Model->checkGoodsOrder($goods, $grow['goods_number'], $grow['sku_val']);
            if ($res !== true) return $this->error($res);
            $price = $GoodsModel->evalPrice($goods, $grow['goods_number'], $grow['sku_val']);//计算需显示的商品价格
            if ($this->is_integral == 0 && $grow['sale_price'] != $price['min_price']){
                return $this->error('商品'.$grow['goods_name'].$grow['sku_name'].'价格发生变化，购物车价格：￥'.$grow['sale_price'].'，当前价格：￥'.$price['min_price']);
            }
            $supplyer_ids[$grow['supplyer_id']] = 1;
            if ($grow['supplyer_id'] > 0 ){//供应商商品计算供货总价
                $inArr['settle_price'] += $grow['settle_price'] * $grow['goods_number'];
            }
            if ($grow['give_integral'] > 0 ){//赠送星券总计
                $inArr['give_integral'] += $grow['give_integral'] * $grow['goods_number'];
            }
            if ($grow['give_dbalance'] > 0 ){//赠送广告费
                $inArr['give_dbalance'] += $grow['give_dbalance'] * $grow['goods_number'];
            }
            if ($grow['use_integral'] > 0 ){//扣减星券总计,组合购买时调用
                $use_integral += $grow['use_integral'] * $grow['goods_number'];
            }
            $allGoodsSn[$grow['goods_sn']] = 1;
            $allGoodsId[$grow['goods_id']] = 1;
            $rec_ids[] = $grow['rec_id'];
        }

        if (empty($supplyer_ids) == false){
            $supplyer_ids = array_keys($supplyer_ids);
            if (count($supplyer_ids) > 1){
                $inArr['is_split'] = 1;//供应商两个以上，需要进行拆单
            }else{
                $inArr['supplyer_id'] = reset($supplyer_ids);//获取唯一的供应商id
            }
        }
      
        $inArr['use_bonus'] = 0;
        if ($used_bonus_id > 0) {//优惠券验证
            $BonusModel = new BonusModel();
            $bonus = $BonusModel->binfo($used_bonus_id);
            if ($bonus['user_id'] != $this->userInfo['user_id']) {
                return $this->error('优惠券出错，请核实.');
            }
            if ($bonus['info']['stauts'] != 1) {
                return $this->error('优惠券无法使用：' . $bonus['info']['stauts_info']);
            }
            if ($cartList['totalGoodsPrice'] < $bonus['info']['min_amount']) {
                return $this->error('选择的优惠券满￥' . $bonus['info']['min_amount'] . '才可以使用，请核实.');
            }
            $inArr['use_bonus'] = $bonus['info']['type_money'];
        }
        $time = time();
        $inArr['order_status'] = 0;
        $inArr['pay_status'] = 0;
        $inArr['shipping_status'] = 0;
        $_log = '生成订单';
        //运费处理
        $shippingFee = $this->Model->evalShippingFee($address, $cartList);
        $shippingFee = reset($shippingFee);//现在只返回默认快递
        $inArr['shipping_fee'] = $shippingFee['shipping_fee'] * 1;
        $inArr['order_amount'] = $cartList['orderTotal'] + $inArr['shipping_fee'] - $inArr['use_bonus'];

        if ($inArr['order_amount'] > 0){
            $pay_id = input('pay_id', 0, 'intval');
            if ($pay_id < 0) return $this->error('请选择支付方式.');
            $PaymentModel = new PaymentModel();
            $paymentList = $PaymentModel->getRows(true);
            $payment = $paymentList[$pay_id];
        }elseif($this->is_integral == 1){//星券支付
            $payment['pay_code'] = 'integral';
            $payment['is_pay'] = 1;
            $payment['pay_name'] = '星券兑换';

        }
        if (empty($payment)) return $this->error('相关支付方式不存在或已停用.');

        if ($payment['pay_code'] == 'balance') {//如果使用余额，判断用户余额是否足够
            if ($inArr['order_amount'] > $this->userInfo['account']['balance_money']) {
                return $this->error('余额不足，请使用其它支付方式.');
            }
        }
        $inArr['order_type'] = $this->is_integral;//订单类型，0普通订单,1星券订单

        if($use_integral > 0){
            if ($use_integral > $this->userInfo['account']['use_integral']) {
                if(($this->userInfo['account']['balance_money']+$this->userInfo['account']['use_integral']) < ($use_integral+$inArr['order_amount'])){
                    return $this->error('余额和星券不足无法兑换');
                }
                //余额不足星券放到余额
                $shengyujfen = $use_integral-$this->userInfo['account']['use_integral'];
                $inArr['order_amount'] = $inArr['order_amount']+$shengyujfen;
                $inArr['use_integral'] = $this->userInfo['account']['use_integral'];
                // return $this->error('星券不足无法兑换，你的星券余额为：'.$this->userInfo['account']['use_integral']);
            }else{
                $inArr['use_integral'] = $use_integral;
            }
            

        }

        $inArr['buyer_message'] = input('buy_msg', '', 'trim');
        $inArr['consignee'] = $address['consignee'];
        $inArr['address'] = $address['address'];
        $inArr['merger_name'] = $address['merger_name'];
        $inArr['province'] = $address['province'];
        $inArr['city'] = $address['city'];
        $inArr['district'] = $address['district'];
        $inArr['street'] = $address['street'];
        $inArr['mobile'] = $address['mobile'];

        $inArr['add_time'] = $time;
        $inArr['user_id'] = $this->userInfo['user_id'];
        $inArr['dividend_role_id'] = $this->userInfo['role_id'];
        $inArr['pay_id'] = $payment['pay_id'];
		$inArr['pay_code'] = $payment['pay_code'];
        $inArr['pay_name'] = $payment['pay_name'];
        $inArr['role_id'] = $goods['role_id'];
        $inArr['is_premiums'] = $goods['is_premiums'];
		$inArr['is_dividend'] = $goods['is_dividend'];
        $inArr['discount'] = $cartList['totalDiscount'];
        $inArr['goods_amount'] = $cartList['totalGoodsPrice'];
        $inArr['buy_goods_sn'] = join(',', array_keys($allGoodsSn));
        $inArr['buy_goods_id'] = join(',', array_keys($allGoodsId));
        $inArr['ipadderss'] = request()->ip();
        $inArr['is_pay'] = $payment['is_pay'];//是否需要支付,1线上支付，0，不需要支付，
        $inArr['is_success'] = 1;//普通订单默认有效，如果拼团默认为0，须拼团成功才会为1
        //执行商品库存和销量处理，后台设置下单减库存或余额支付时执行
        $shop_reduce_stock = settings('shop_reduce_stock');
        $inArr['is_stock'] = $shop_reduce_stock;
        Db::startTrans();//启动事务
        $OrderModel = new OrderModel();
        $inArr['order_sn'] = $OrderModel->getOrderSn();
//        dump($inArr);exit;
        $res = $OrderModel->save($inArr);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return $this->error('未知原因，订单写入失败.');
        }
        $order_id = $OrderModel->order_id;

        $inArr['order_id'] = $order_id;
        $res = $OrderModel->_log($inArr,$_log);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return $this->error('未知原因，订单日志写入失败.');
        }
        //使用星券，下单即扣除
        // if ($use_integral > 0){
        //     $changedata['use_integral'] = $use_integral * -1;
        //     $changedata['change_desc'] = '购物抵扣星券';
        //     $changedata['change_type'] = 3;
        //     $changedata['by_id'] = $order_id;
        //     $res = (new AccountLogModel)->change($changedata, $this->userInfo['user_id'], false);
        //     if ($res !== true) {
        //         Db::rollback();// 回滚事务
        //         return $this->error('扣减星券失败失败.');
        //     }
        // }

        // 代理商产品更新代理申请订单信息
        $goods = Db::table('shop_goods')->where('goods_id',$inArr['buy_goods_id'])->where('is_type','<>',0)->find();
        if($goods){
            if(Db::table('user_region')->where('user_id',$this->userInfo['user_id'])->where('status',0)->count('id')){
                $res = Db::table('user_region')->where('user_id',$this->userInfo['user_id'])->where('level',$goods['is_type'])->update(['order_id'=>$order_id]);
                if(empty($res)){
                    Db::rollback();// 回滚事务
                    return $this->error('系统繁忙');
                }   
            }
          
        }

        //执行扣库存
        if ($inArr['is_stock'] == 1) {
            if ($inArr['order_type'] == 1) {//星券订单
                $res = (new \app\integral\model\IntegralGoodsListModel)->evalGoodsStore($cartList['goodsList']);
            } else {
                $res = $GoodsModel->evalGoodsStore($cartList['goodsList']);
            }
            if ($res != true) {
                Db::rollback();//回滚
                return $this->error('扣库存失败');
            }
        }
        //end
        //处理优惠券
        if ($used_bonus_id > 0) {
            $upArr = array();
            $upArr['user_id'] = $this->userInfo['user_id'];
            $upArr['used_time'] = $time;
            $upArr['order_id'] = $order_id;
            $upArr['order_sn'] = $inArr['order_sn'];
            $BonusListModel = new BonusListModel();
            $res = $BonusListModel->where('bonus_id', $used_bonus_id)->update($upArr);
            if ($res < 1) {
                Db::rollback();// 回滚事务
                return $this->error('未知错误，修改优惠券失败.');
            }
        }
        //end
        $this->addOrderGoods($order_id,$rec_ids);//写入商品
        Db::commit();// 提交事务
        $return['order_id'] = $order_id;
        $return['code'] = 1;
        return $this->ajaxReturn($return);
    }
    /*------------------------------------------------------ */
    //-- 写入订单商品
    //--- 这里可能有bug,如果用户同时执行多次，商品有可能发生错误
    /*------------------------------------------------------ */
    public function addOrderGoods($order_id,$recids)
    {
        $add_time = time();
        $sql = "INSERT INTO shop_order_goods (" .
            "order_id,brand_id,cid,supplyer_id,goods_id,sku_id,sku_val,sku_name,goods_name,pic,goods_sn,goods_number,market_price,shop_price,sale_price,settle_price,goods_weight,discount,add_time,user_id,give_integral,give_dbalance,use_integral,is_dividend,buy_again_discount)" .
            " SELECT '$order_id',brand_id,cid,supplyer_id,goods_id,sku_id,sku_val,sku_name,goods_name,pic,goods_sn,goods_number,market_price,shop_price,sale_price,settle_price,goods_weight,discount,'$add_time',user_id,give_integral,give_dbalance,use_integral,is_dividend,buy_again_discount" .
            " FROM  shop_cart  WHERE ";
        $sql .= " user_id = '" . $this->userInfo['user_id'] . "' AND is_select = 1 ";
        $sql .= " AND is_integral =  " . $this->is_integral;
		$sql .= " AND rec_id IN (".join(',',$recids).") ";
        $sql .= " order by rec_id asc";
        $res = Db::execute($sql);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return $this->error('未知原因，订单商品写入失败.');
        }
        $where[] = ['user_id', '=', $this->userInfo['user_id']];
        $where[] = ['is_select', '=', 1];
        $where[] = ['is_integral', '=', $this->is_integral];
		$where[] = ['rec_id', 'in', $recids];
        $this->Model->where($where)->delete();// 清理购物车的商品
        $this->Model->cleanMemcache();
        return $res;
    }
}
